/*
 * ContextClass.java
 *
 * Created on 14 de diciembre de 2004, 16:29
 */

package ocp.context;

import java.util.Vector;
import java.util.Enumeration;

/**
 * This object represents a class object in our context. This objects are the contextual
 * representations of certain real-life objects, such as a chair, or some contextual abstract
 * information, like the location of an user.
 * @author  nacho
 */
public class ContextClass implements java.io.Serializable  {
    
    /** the superclas of this object's represented class. null implies this class has no father */
    protected ContextClass superclass = null;
    
    /** vector of attributes of this context */
    protected Vector attributeDefinitions;
    
    /** vector of property definitions of this context */
    protected Vector propertyDefinitions;
    
    /** the name of the ContextClass */
    protected String name;
    
    /** vector of equivalent classes */
    protected Vector equivalentClasses;
    
    /** vector of properties this class is intersection of */
    protected Vector intersectionOf;
    
    protected Boolean historical;    
    
    /** Creates a new instance of ContextClass */
    public ContextClass(String name, ContextClass superclass) {
        attributeDefinitions = new Vector();
        equivalentClasses = new Vector();
        intersectionOf = new Vector();
        propertyDefinitions = new Vector();
        this.name = name;
        this.superclass = superclass;
        historical = new Boolean(false);
    }
    
    public ContextClass(String name) {
        this(name, null);
    }
    
    /** Sets the superclass of this context class */
    public ContextClass setSuperClass(ContextClass superclass) {
        this.superclass = superclass;
        return this;
    }
    
    /** returns the class this class heritages from (ontologicaly) */
    public ContextClass subclassOf()    { return superclass; }
    
    /** returns the name of this ContextClass */
    public String getName()             { return name; }
    
    /** adds an atribute definition to this context class 
     * @return this in order to concatenate.
     */
    public ContextClass addAttributeDefinition(AttributeDefinition att) { attributeDefinitions.addElement(att); return this;}
    
    public ContextClass removeAttributeDefinition(AttributeDefinition att) { attributeDefinitions.removeElement(att); return this;}
    
    public ContextClass removeAllAttributeDefinitions() { attributeDefinitions.removeAllElements(); return this; }
    
    public java.util.Enumeration getAttributeDefinitions() { return attributeDefinitions.elements(); }
    
    public ContextClass addPropertyDefinition(ContextPropertyDefinition cpd) {propertyDefinitions.addElement(cpd); return this; }
    
    public ContextClass removePropertyDefinition(ContextPropertyDefinition cpd) { propertyDefinitions.removeElement(cpd); return this; }
    
    public ContextClass removeAllPropertyDefinitions() { propertyDefinitions.removeAllElements(); return this; }
    
    public java.util.Enumeration getPropertyDefinitions() { return propertyDefinitions.elements(); }

    
    public ContextClass isEquivalentTo (ContextClass cc) {
        if (!equivalentClasses.contains(cc))
            equivalentClasses.addElement(cc);
        return this;
    }
    
    public ContextClass isNotEquivalentTo(ContextClass cc) {
        equivalentClasses.removeElement(cc);
        return this;
    }
    
    public ContextClass isIntersectionOf(ContextPropertyDefinition cpd) { 
        if (!intersectionOf.contains(cpd))
            intersectionOf.addElement(cpd);
        return this;
    }
    
    public ContextClass isNotIntersectionOf(ContextPropertyDefinition cpd) {
        intersectionOf.removeElement(cpd);
        return this;
    }
    
    /** checks if a given ContextualInformation is an instance of this class
     * @return true if ci fulfils this ContextClass definition, false otherwise.
     */
    public boolean validate(ContextualInformation ci) {
        // TODO
        return true;
    }
    
    public java.util.Enumeration getEquivalentClasses() { return equivalentClasses.elements(); }
    
    public java.util.Enumeration getIntersections()     { return intersectionOf.elements(); }
    
    public boolean equals(Object o) {
        ContextClass cc;
        if (o instanceof ContextClass) {
            cc = (ContextClass) o;
            if (cc.getName().equals(name)) return true;
            for (Enumeration e1 = equivalentClasses.elements(); e1.hasMoreElements();)
            {
                ContextClass temp = (ContextClass) e1.nextElement();
                if (temp.getName().equals(cc.getName())) return true;
            }
            return false;
        }
        return false;
    }
    
    public String toXML() {
        String xml = "<ContextClass>\n";
        xml += "\t<Name>"+name+"</Name>\n";
        xml += "\t<SuperClass>"+(superclass == null?"none":superclass.getName())+"</SuperClass>\n";
        for (Enumeration e1 = attributeDefinitions.elements(); e1.hasMoreElements(); )
            xml += "\t\t"+ ((AttributeDefinition) e1.nextElement() ).toXML() + "\n";
        for (Enumeration e2 = propertyDefinitions.elements(); e2.hasMoreElements(); )
            xml += "\t\t"+ ((ContextPropertyDefinition ) e2.nextElement()).toXML() + "\n";
        xml += "\t</ContextClass>\n";
        
        return xml;
        
    }
   
    public Boolean getHistorical() {
        return historical;
    }

    public void setHistorical(Boolean historical) {
        this.historical = historical;
    }
    
    
}
